﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;

namespace Skynet.WebSupport.Views.PerformanceMgm
{
    [System.ComponentModel.DataObject]
    public class PerformanceMgmPresenter : WebFoundation.Views.Master.MasterPresenter<IPerformanceMgmView>
    {
        private enum ADO_Net_Performance_Counters
        {
            NumberOfActiveConnectionPools,
            NumberOfReclaimedConnections,
            HardConnectsPerSecond,
            HardDisconnectsPerSecond,
            NumberOfActiveConnectionPoolGroups,
            NumberOfInactiveConnectionPoolGroups,
            NumberOfInactiveConnectionPools,
            NumberOfNonPooledConnections,
            NumberOfPooledConnections,
            NumberOfStasisConnections
            // The following performance counters are more expensive to track.
            // Enable ConnectionPoolPerformanceCounterDetail in your config file.
            //     SoftConnectsPerSecond
            //     SoftDisconnectsPerSecond
            //     NumberOfActiveConnections
            //     NumberOfFreeConnections
        }

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern int GetCurrentProcessId();
        private PerformanceCounter[] PerfCounters = new PerformanceCounter[10];
        public override void OnViewInitialized()
        {

            this.View.InstanceName = GetInstanceName();
            SetUpPerformanceCounters();
            LoadCounters();
        }
        public override void OnViewLoaded()
        {
            this.View.InstanceName = GetInstanceName();
            SetUpPerformanceCounters();
            
        }

        
        public void LoadCounters()
        {                        
            var list = new List<Tuple<string,float>>();
            var ran = new Random();
            var count =  0 ;
            foreach (var item in this.PerfCounters)
            {
                count++;
                list.Add(new Tuple<string,float>("text" +  count.ToString(), count ));
            }
            this.View.AdoPerformanceCounters = list;
        }
        private string GetInstanceName()
        {
            var instanceName = AppDomain.CurrentDomain.FriendlyName.ToString().Replace('(', '[').Replace(')', ']').Replace('#', '_').Replace('/', '_').Replace('\\', '_');
            string pid = GetCurrentProcessId().ToString();
            instanceName = instanceName + "[" + pid + "]";
            return instanceName; 
        }
        private void SetUpPerformanceCounters()
        {
            this.PerfCounters = new PerformanceCounter[10];
            string instanceName = GetInstanceName();
            Type apc = typeof(ADO_Net_Performance_Counters);
            int i = 0;
            foreach (string s in Enum.GetNames(apc))
            {
                this.PerfCounters[i] = new PerformanceCounter();
                this.PerfCounters[i].CategoryName = ".NET Data Provider for SqlServer";
                this.PerfCounters[i].CounterName = s;
                this.PerfCounters[i].InstanceName = instanceName;
                i++;
            }
        }
    }
}
